package com.thd.springboottest.jpa.controller;

import com.thd.springboottest.jpa.entity.School;
import com.thd.springboottest.jpa.entity.User;
import com.thd.springboottest.jpa.repository.SchoolRepository;
import com.thd.springboottest.jpa.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * com.thd.springboottest.jpa.controller.JpaController
 *
 * @author: wanglei62
 * @DATE: 2022/7/5 16:22
 **/
@Controller
@RequestMapping("/jpa")
public class JpaController {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private SchoolRepository schoolRepository;
    private Logger logger = LoggerFactory.getLogger(JpaController.class);
    @RequestMapping("/test")
    @ResponseBody
    public ResponseEntity test(){
        System.out.println("SUCCESS");

        logger.info("success {},success {}","SUCCESS","1");
        return ResponseEntity.ok("SUCCESS");
    }


    /**
     * 保存用户
     * @return
     */
    @RequestMapping("/saveUser")
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/jpa/saveUser
    public ResponseEntity<User> saveUser(){

        User u = new User();
        u.setId(UUID.randomUUID().toString());
        u.setAge(new Random().nextInt(100));
        u.setBirthday(new Date());
        u.setCreateAt(new Date());
        u.setName("thd");
        userRepository.save(u);
        logger.info("保存用户成功");
        return ResponseEntity.ok(u);

    }

    /**
     * 根据用户查询
     * @param id
     * @return
     */
    @RequestMapping("/queryUserById/{id}")
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/jpa/queryUserById/1001
    public ResponseEntity<User> queryUserById(@PathVariable String id){
        Optional<User> user = userRepository.findById(id);
        logger.info("查询用户成功");
        return ResponseEntity.ok(user.get());

    }


    /**
     * 列表查询
     */
    @RequestMapping("/queryUser")
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/jpa/queryUser
    public ResponseEntity<List<User>> queryUser(){
        List<User> list = userRepository.findAll();
        return ResponseEntity.ok(list);

    }

    /**
     * JPA形式的接口条件查询
     */
    @RequestMapping("/queryUserByJpa")
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/jpa/queryUserByJpa
    public ResponseEntity<Map<String,List<User>>> queryUserByJpa(){

        Map<String,List<User>> r = new HashMap<String,List<User>>();
        logger.info("==根据姓名查询==");
        List<User> listByName = userRepository.findByName("thd");
        System.out.println(listByName);
        r.put("根据姓名查询",listByName);


        logger.info("==根据姓名和年龄查询==");
        List<User> listByNameAndAge = userRepository.findByNameAndAge("thd",47);
        System.out.println(listByNameAndAge);
        r.put("根据姓名和年龄查询",listByNameAndAge);


        logger.info("==根据姓名或年龄查询==");
        List<User> listByNameOrAge = userRepository.findByNameOrAge("thd",47);
        System.out.println(listByNameOrAge);
        r.put("根据姓名或年龄查询",listByNameOrAge);


        logger.info("==自定义查询==");
        List<User> listByNativeSQL = userRepository.queryByNativeSQL("t");
        System.out.println(listByNativeSQL);
        r.put("自定义查询",listByNativeSQL);

        List<User> listByNativeSQL2 = userRepository.queryByNativeSQL2("t");
        System.out.println(listByNativeSQL2);
        r.put("自定义查询2",listByNativeSQL2);

        logger.info("==分页查询==");
        Integer page = 1; // 第几页
        Integer size = 2; // 每页多少行



        Sort s = Sort.by(Sort.Order.asc("name"));
        // 分页且排序
        Pageable pageRequest = PageRequest.of(page - 1, size,s);

        // 下面是仅仅分页不排序
//        Pageable pageRequest = PageRequest.of(page - 1, size);
        Page<User> pageList = userRepository.findAll(pageRequest);
        System.out.println(pageList);
        return ResponseEntity.ok(r);

    }



    /**
     * 列表查询
     */
    @RequestMapping("/querySchool")
    @ResponseBody
    // url : http://127.0.0.1:8899/thd/jpa/querySchool
    public ResponseEntity<List<School>> querySchool(){
        List<School> list = schoolRepository.findAll();
        return ResponseEntity.ok(list);

    }
}
